import 'package:app/api/account/index.dart';
import 'package:app/util/StringUtils.dart';
import 'package:flutter/material.dart';
import '../../components/index.dart';
import 'package:app/page/home/mine.dart';
import 'package:app/page/home/home.dart';

class Index extends StatefulWidget{

  const Index({super.key});

  @override
  State<StatefulWidget> createState()  => _IndexState();

}

/// 首页
class _IndexState extends State<Index>{

  int _currentIndex = 0;

  final List<Widget> _pages = [];

  void onTap(index){
    setState(() => _currentIndex = index);
  }

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_){
      userInfo();
    });
  }

  /// 获取用户信息
  userInfo(){
    Account.of(context).userInfo().then((res){
      if(StringUtils.isNotNull(res) && StringUtils.isNotNull(res.data)
          && StringUtils.isNotNull(res.data['data'])){
        setState((){
          _pages.add(Home(userName: res.data['data']['userName']));
          _pages.add(Mine(userName: res.data['data']['userName']));
        });
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: IndexedStack(
        index: _currentIndex,
        children: _pages,
      ),
      bottomNavigationBar: Navigation(
        context: context,
        currentIndex: _currentIndex, onTap: onTap),
    );
  }
}